package cn.lisens.common.shiro.realm;

import java.util.HashSet;
import java.util.Set;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.stereotype.Component;

@Component
public class MyRealm extends AuthorizingRealm {


	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		String username = (String)token.getPrincipal();
		System.out.println("登陆用户名："+username);
		String password = "lyxx";//token.getPrincipal();
		AuthenticationInfo info = new SimpleAuthenticationInfo(username, password, "MyRealm!!!!");
		return info;
	}
	
	// 授权方法，授权是在认证之后的操作
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
	    return null;
//		//1. 获取认证用户的信息
//		String user = (String) principals.getPrimaryPrincipal();
//
//	    //2. 基于用户信息获取当前用户拥有的角色。
//	    Set<String> roleSet = this.findRolesByUser();
//
//	    //3. 基于用户拥有的角色查询权限信息
//	    Set<String> permSet = this.findPermsByRoleSet(roleSet);
//
//	    //4. 声明AuthorizationInfo对象作为返回值，传入角色信息和权限信息
//	    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//	    info.setRoles(roleSet);
//	    info.setStringPermissions(permSet);
//
//	    //5. 返回
//	    return info;
	}

	private Set<String> findPermsByRoleSet(Set<String> roleSet) {
	    Set<String> set = new HashSet<>();
	    set.add("user:add");
	    set.add("user:update");
	    return set;
	}

	private Set<String> findRolesByUser() {
	    Set<String> set = new HashSet<>();
	    set.add("超级管理员");
	    set.add("运营");
	    return set;
	}



}
